package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    //获取 session 对象
    public static HttpSession getSession() {
        //使用上下文RequestContextHolder 可以在任意的地方获取 request response  session 对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }


    //获取当前登录用户
    public static Employee getCurrentUser() {


        return (Employee) getSession().getAttribute(USER_IN_SESSION);

    }

    //设置当前登录用户的 员工 到session中
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(USER_IN_SESSION, employee);

    }

    //获取当前登录用户的权限表达式
    public static List<String> getExpression() {
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }

    //设置当前登录的员工的权限表达式列表
    public static void setExpression(List<String> expression) {

        getSession().setAttribute(EXPRESSION_IN_SESSION, expression);

    }

}
